package com.zbbt.sportslottery.ui.activity;

import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ButtonRectangle;
import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.UserInfo;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.widget.ClearEditText;
import com.zbbt.sportslottery.util.KeyUtil;
import com.zbbt.sportslottery.util.Md5;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengUtil;
import com.zbbt.sportslottery.util.ViewUtil;

/**
 * 
 * @ClassName: LoginActivity
 * @Description: 登陆页面
 * @author: yangwenquan
 * @date: 2015年2月6日 下午3:26:58
 * @version: V1.0
 */
public class LoginActivity extends SimpleBaseActivity {
	private ClearEditText et_loginName;
	private ClearEditText et_loginPsd;
	private ButtonRectangle registerButton;
	private ButtonRectangle loginButton;
	private String usernameContent;
	private String passwordContent;
	private View mRootview;
	private LinearLayout linearLayoutImg;
	private TextView tv_forgetPwd;// 忘记密码
	private ProgressBarCircularIndetermininate mProgressBar;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_login);
	}

	@Override
	public void initView() {
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.mProgressBar);
		et_loginName = (ClearEditText) findViewById(R.id.username_edittext);
		et_loginPsd = (ClearEditText) findViewById(R.id.password_edittext);
		linearLayoutImg = (LinearLayout) findViewById(R.id.ll_img1);
		registerButton = (ButtonRectangle) findViewById(R.id.goto_registerbutton);
		registerButton.setTextColor(getResources().getColor(R.color.white));
		registerButton.setTextSize(20);
		loginButton = (ButtonRectangle) findViewById(R.id.login_button);
		loginButton.setTextColor(getResources().getColor(R.color.white));
		loginButton.setTextSize(20);
		mRootview = findViewById(R.id.mRootview);
		tv_forgetPwd = (TextView) findViewById(R.id.forget_pwd);
		// 从文件中获取保存的数据
		usernameContent = SharePreferUtil.getString(Constant.USER_NAME, "");
		passwordContent = SharePreferUtil.getString(Constant.PASSWORD, "");
		et_loginName.setText(usernameContent);
		et_loginPsd.setText(passwordContent);
		ViewUtil.listenerSoftInput(linearLayoutImg, mRootview);
		et_loginName.addTextChangedListener(mTextWatcher);
	}

	TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			final String s1 = et_loginName.getText().toString();
			if (!s1.equals("")) {
				if (!Pattern.matches("^[a-zA-Z0-9\u4e00-\u9fa5]+$", s1)) {
					et_loginName.setText("");
					et_loginName.setShakeAnimation();
					ToastUtil.showLong("用户名只能包含数字");
				}
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

		@Override
		public void afterTextChanged(Editable s) {

		}
	};

	@Override
	protected void setListener() {
		loginButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		tv_forgetPwd.setOnClickListener(this);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.login_button:
			String usernameContent = et_loginName.getText().toString().trim();
			String passwordContent = et_loginPsd.getText().toString().trim();
			if (TextUtils.isEmpty(usernameContent)) {
				ToastUtil.showLong("账号不能为空");
				et_loginName.setShakeAnimation();
				return;
			}
//			if (usernameContent.length() < 6 || usernameContent.length() > 16) {
//				ToastUtil.showLong("您输入的账号应在6~16个字符");
//				et_loginName.setShakeAnimation();
//				return;
//			}
			if (TextUtils.isEmpty(passwordContent)) {
				ToastUtil.showLong("密码不能为空");
				et_loginPsd.setShakeAnimation();
				return;
			}
			if (passwordContent.length() > 16 || passwordContent.length() < 6) {
				ToastUtil.showLong("您输入的密码应在6~16个字符");
				et_loginPsd.setShakeAnimation();
				return;
			}
			httpData(usernameContent, passwordContent);
			break;
		case R.id.goto_registerbutton:
			Intent intent = new Intent(context, RegisterActivity.class);
			startActivityForResult(intent, Constant.FINISH_THIS_ACTIVITY);
			break;
		case R.id.forget_pwd:
			goActivity(ForgetPwdActivity.class);
			break;
		default:
			break;
		}
	}

	private void httpData(final String usernameContent, final String passwordContent) {
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("mobile", usernameContent);
		String passwordContentMD5 = Md5.getMD5(passwordContent);
		jsonObject.addProperty("password", passwordContentMD5);
		jsonObject.addProperty("loginType", "1");// 登陆类型 1为本地登陆
		jsonObject.addProperty("firstLoginAppid", App.PARAM_APPID);
		mProgressBar.setVisibility(View.VISIBLE);
		ZbHttpClient.postNewApi(ServerConfig.API_LOGIN, jsonObject, new TypeGsonHttpHandler<UserInfo>(true) {

			@Override
			public void onSuccess(UserInfo data) {
				mProgressBar.setVisibility(View.GONE);
				if (data != null) {
					// 登录成功，保存的数据
				    UmengUtil.onEvent(context, Constant.UMENG_PERSON_LOGIN);
					SharePreferUtil.putString(Constant.USER_NAME, usernameContent);
					SharePreferUtil.putString(Constant.PASSWORD, passwordContent);
					SharePreferUtil.putString(Constant.USER_ID, data.getUserId());
					SharePreferUtil.putString(Constant.USER_TYPE, data.getUserType() + "");
					SharePreferUtil.putString(Constant.USER_SCORE, data.getTotalSource() + "");
					SharePreferUtil.putString(Constant.USER_PHOTO_IMG, data.getAvatar() + "");
					Intent loginSuccess = new Intent(Constant.INTENT_ACTION_LOGIN_SUCCESS);
					sendBroadcast(loginSuccess);// 发广播
					finish();
					App.isLogin=true;
					Intent intent = new Intent(context, MainActivity.class);
					intent.putExtra(KeyUtil.GotoType, KeyUtil.GOTO_FRAGMENT);
					intent.putExtra(KeyUtil.FragmentKey, KeyUtil.FRAGMENT_USERCENTER);
					startActivity(intent);
				}
			}

			@Override
			public void onFailure(String errorCode, String errorMsg) {
				mProgressBar.setVisibility(View.GONE);
				ToastUtil.showLong(errorMsg);
			}
		});
	}

}
